package com.daJiao.service.impl;

import com.google.code.kaptcha.Producer;
import com.daJiao.client.SysSmsClient;
import com.daJiao.constants.RedisKeyPrefix;
import com.daJiao.enums.SmsTemplateEnum;
import com.daJiao.exception.BusinessException;
import com.daJiao.service.RedisService;
import com.daJiao.service.ValidateCodeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>Title:	  compliance-boot <br/> </p>
 * <p>Description TODO <br/> </p>
 * <p>Company:    医百科技有限公司  <br/> </p>
 *
 * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>  <br/>
 * @Date 2020/9/14 15:36
 */
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {


    @Resource
    private Producer defaultKaptcha;

    @Resource
    private RedisService redisService;

    @Resource
    private SysSmsClient sysSmsClient;


    @Override
    public String generateSmsCode(String mobile) {
        boolean exists = redisService.exists(RedisKeyPrefix.SMS_PREFIX + mobile);
        if (exists) {
            throw new BusinessException("获取短信验证码太频繁，请稍后重试");
        }
        // 生成验证码字符串并保存到redis中
        String createText = defaultKaptcha.createText();
        redisService.set(RedisKeyPrefix.SMS_PREFIX + mobile, createText, RedisKeyPrefix.EXPIRE_TIME_MINUTE);
        // 调用短信服务发送短信验证码
        sysSmsClient.sendSms(new String[]{"+86"+mobile}, SmsTemplateEnum.TEMPLATE_1.getKey(), new String[]{createText,"1"});
        return createText;
    }
}
